package byggemarked;

import Models.*;
import java.util.Date;

/**
 * @author Morten
 */
public class Tools
{
	public static String makeNiceStringFromDate(Date date)
    {
		if(date != null)
		{
			String month = "";
			switch(date.getMonth())
			{
				case 0:
					month = "januar";
					break;
				case 1:
					month = "februar";
					break;
				case 2:
					month = "marts";
					break;
				case 3:
					month = "april";
					break;
				case 4:
					month = "maj";
					break;
				case 5:
					month = "juni";
					break;
				case 6:
					month = "juli";
					break;
				case 7:
					month = "august";
					break;
				case 8:
					month = "september";
					break;
				case 9:
					month = "oktober";
					break;
				case 10:
					month = "november";
					break;
				case 11:
					month = "december";
					break;
			}
			int year = date.getYear()+1900;
			return date.getDate()+". "+month+" "+year;
		}
		else
		{
			return "";
		}
    }

	public static String makeStringFromDate(Date date)
	{
		if(date != null)
		{
			String day = String.valueOf(date.getDate());
			String month = String.valueOf(date.getMonth()+1);
			String year = String.valueOf(date.getYear()+1900);
			if(day.length() == 1)
			{
				day = "0"+day;
			}
			if(month.length() == 1)
			{
				month = "0"+month;
			}
			return day+"-"+month+"-"+year;
		}
		else
		{
			return "";
		}
	}

	public static Date makeDateFromString(String dateString) throws NumberFormatException
	{
		//Format: 26-05-2009
		if(dateString.length() < 10)
		{ throw new NumberFormatException(); }

		int day = Integer.parseInt(dateString.substring(0, 2));
		int month = Integer.parseInt(dateString.substring(3, 5))-1;
		int year = Integer.parseInt(dateString.substring(6, 10))-1900;
		return new Date(year, month, day);
	}

	public static String getNameOrCompanyName(Customer customer)
	{
		if(customer instanceof PrivateCustomer)
		{
			PrivateCustomer pCustomer = (PrivateCustomer)customer;
			return pCustomer.getName();
		}
		else
		{
			BusinessCustomer bCustomer = (BusinessCustomer)customer;
			return bCustomer.getCompanyName();
		}
	}
}
